package com.songlanyun.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.entity.GoodsGroupRelation;
import com.songlanyun.modules.goods.model.dto.GoodsStatusDTO;
import com.songlanyun.modules.goods.service.GoodsGroupRelationService;
import com.songlanyun.modules.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.songlanyun.modules.goods.dao.GoodsAuthShopDao;
import com.songlanyun.modules.goods.entity.GoodsAuthShop;
import com.songlanyun.modules.goods.service.GoodsAuthShopService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("goodsAuthShopService")
public class GoodsAuthShopServiceImpl extends ServiceImpl<GoodsAuthShopDao, GoodsAuthShop> implements GoodsAuthShopService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsGroupRelationService goodsGroupRelationService;

    /**
     * @param shopId
     * @param goods
     * @return
     */
    @Override
    public GoodsAuthShop add(Long shopId, Goods goods) {
        GoodsAuthShop authorizeInfo = getAuthorizeInfo(shopId, goods.getId());
        if (authorizeInfo != null) {
            throw new RRException(GoodsExceptionEnum.GOODS_AUTH_SHOP_REPEAT);
        }

        GoodsAuthShop relationEntity = new GoodsAuthShop();
        relationEntity.setGoodsId(goods.getId());
        relationEntity.setAdded(true);
        relationEntity.setAddedStatusTime(new Date());
        relationEntity.setPublishShopId(goods.getShopId());
        relationEntity.setAuthorizeShopId(shopId);
        this.baseMapper.insert(relationEntity);
        return relationEntity;


    }

    @Override
    public void auth(Long shopId, Long goodsId) {
        Goods goods = goodsService.getById(goodsId);
        if (goods == null) {
            throw new RRException(GoodsExceptionEnum.GOODS_NOT_EXISTS);
        }

        if (goods.loadShop() == null || !goods.loadShop().getPlatformShop()) {
            throw new RRException(GoodsExceptionEnum.GOODS_NOT_EXISTS);
        }
        this.add(shopId, goods);
    }

    /**
     * 取消商品的某个店铺的经营权限
     *
     * @param shopId
     * @param goodsId
     */
    @Override
    public void cancelAuth(Long shopId, Long goodsId) {
        GoodsAuthShop godsAuthShop = getAuthorizeInfo(shopId, goodsId);
        if (godsAuthShop == null) {
            return;
        }
        if (!godsAuthShop.isProxy()) {
            throw new RRException(GoodsExceptionEnum.NOT_CANCEL_SELF_GOODS);
        }

        /**
         * 先移除商品分组
         */
        LambdaQueryWrapper<GoodsGroupRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsGroupRelation::getGoodsAuthShopId, godsAuthShop.getId());
        goodsGroupRelationService.remove(wrapper);

        /**
         * 取消商品的某个店铺的经营权限
         */
        this.removeById(godsAuthShop.getId());

    }


    @Override
    public void added(Long shopId, GoodsStatusDTO dto) {

        GoodsAuthShop authorizeInfo = this.getAuthorizeInfo(shopId, dto.getGoodsId());
        if (authorizeInfo == null) {
            throw new RRException(GoodsExceptionEnum.NOT_PROXY_AUTH);
        }

        authorizeInfo.setAdded(dto.getAdded());
        authorizeInfo.setAddedStatusTime(new Date());
        this.updateById(authorizeInfo);

        /**
         * 如果平台商品下架，所有代销的也要下架
         */
        if (!authorizeInfo.isProxy()) {
            if (!authorizeInfo.isAdded()) {
                LambdaQueryWrapper<GoodsAuthShop> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(GoodsAuthShop::getGoodsId, authorizeInfo.getGoodsId());
                List<GoodsAuthShop> list = this.list(wrapper);
                //排除自己
                List<GoodsAuthShop> other = list.stream().filter(o -> {
                    return !o.getId().equals(authorizeInfo.getId());
                }).collect(Collectors.toList());

                if (other.size() > 0) {
                    other.stream().forEach(o -> {
                        o.setAdded(false);
                        o.setAddedStatusTime(new Date());
                    });
                    this.updateBatchById(other);
                }

            }
        }
    }

    @Override
    public void removeCommonShopAuth(Long shopId, Long goodsId) {
        this.remove(new LambdaQueryWrapper<GoodsAuthShop>()
                .eq(GoodsAuthShop::getGoodsId, goodsId)
                .ne(GoodsAuthShop::getAuthorizeShopId, shopId));
    }


    /**
     * 查询商品某个店铺的经营权限
     *
     * @param shopId
     * @param goodsId
     * @return
     */
    @Override
    public GoodsAuthShop getAuthorizeInfo(Long shopId, Long goodsId) {
        GoodsAuthShop entity = this.baseMapper.getAuthorizeInfo(shopId, goodsId);
        return entity;
    }

    /**
     * 查询某个商品店直营权限
     *
     * @param goodsId
     * @return
     */
    @Override
    public GoodsAuthShop getDirectSaleAuthorizeInfo(Long goodsId) {
        GoodsAuthShop entity = this.baseMapper.getDirectSaleAuthorizeInfo(goodsId);
        return entity;
    }
}
